precision highp float;
//precision lowp sampler2D;

uniform sampler2D u_tex;

varying highp vec2 v_uv[9];


vec4 texture2Dcustom (sampler2D tex, vec2 uv) {
    vec4 c = texture2D(tex, uv);
    return vec4(c.rgb / (c.a + 0.0001), c.a);
}
float rgb2lum (vec3 RGB) {
    const vec3 W = vec3(0.299, 0.587, 0.114);
    return dot(RGB, W);
}

void main () {
    mediump vec4 cc = texture2Dcustom(u_tex, v_uv[0]);
    mediump vec3 lb = texture2Dcustom(u_tex, v_uv[1]).rgb;
    mediump vec3 rt = texture2Dcustom(u_tex, v_uv[2]).rgb;
    mediump vec3 lc = texture2Dcustom(u_tex, v_uv[3]).rgb;
    mediump vec3 rc = texture2Dcustom(u_tex, v_uv[4]).rgb;
    mediump vec3 lt = texture2Dcustom(u_tex, v_uv[5]).rgb;
    mediump vec3 rb = texture2Dcustom(u_tex, v_uv[6]).rgb;
    mediump vec3 cb = texture2Dcustom(u_tex, v_uv[7]).rgb;
    mediump vec3 ct = texture2Dcustom(u_tex, v_uv[8]).rgb;

    mediump vec3 dH = (rb + rc + rc + rt) - (lb + lc + lc + lt);
    mediump vec3 dV = (lt + ct + ct + rt) - (lb + cb + cb + rb);
    mediump vec3 dS = clamp(sqrt(dH * dH + dV * dV), 0.0, 1.0);

    vec3 rgb = cc.rgb * dS * rgb2lum(dS);
    gl_FragColor = vec4(rgb, cc.a);
    // No premultiply
}